package com.hxf.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dto.AdminRoleDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.RoleDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.impl.BaseServiceImpl;
import com.hxf.service.user.UserService;
import com.hxf.web.vo.UserRoleVo;

 
/**
 * service会员实现类
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService {

	@Override
	public List<UserDto> findByUser(UserDto userDto, int targetPage, int pageSize)throws ServiceException {
		try {
			return super.daoFacade.getUserDao().findByUser(userDto, targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public List<UserRoleVo> findByUserAddRole(AdminRoleDto urDto,Integer userId) throws ServiceException {
		try {
			  List<RoleDto> roleList = super.daoFacade.getRoleDao().findByRole(null);
			  List<RoleDto> selectRoleList = super.daoFacade.getUserDao().findByUserRole(urDto);//查询用户权限 要设置的用户
			  UserDto userDto = super.daoFacade.getUserDao().selectUserOrAdmin(userId);
			  RoleDto rd = null;
			  RoleDto srd = null;
			  UserRoleVo urVo = null;
			  List<UserRoleVo> urList = new ArrayList<UserRoleVo>();
			  //上级为空,为超级管理员，显示所有角色
			  if(null !=userDto && (null == userDto.getParentId()||"null".equals(userDto.getParentId()))
					  && (null==userDto.getParentUserId() || "null".equals(userDto.getParentUserId()))){
				  
				  if (roleList != null && roleList.size() > 0) {
					  for (int i = 0;i < roleList.size(); i++) {
						  rd = roleList.get(i);
						  urVo = new UserRoleVo();
						  urVo.setRoleId(rd.getRoleId());
						  urVo.setRoleName(rd.getRoleName());
						  urVo.setRemark(rd.getRemark());
						  urVo.setStatus(rd.getStatus());
						  urVo.setSelectStatus(false);
						  if (selectRoleList != null && selectRoleList.size() > 0) {
							  for (int j = 0;j < selectRoleList.size(); j++) {
								  srd = selectRoleList.get(j);
								  if (rd.getRoleId() == srd.getRoleId()) {
									  urVo.setSelectStatus(true);
								  }
							  }
						  } 
						  urList.add(urVo); 
					  }
				  }
				  return urList;
			  }else{//不是超级管理员,只显示餐厅原本给的权限
				  urDto.setUserId(userId); //设置为 当前餐厅登陆的 ID
				  List<RoleDto> selectRoleListParent =  super.daoFacade.getUserDao().findByUserRole(urDto);
				  if(null != selectRoleList && selectRoleList.size()>0){
					  for(int i=0;i<selectRoleListParent.size();i++){
						  rd = selectRoleListParent.get(i);
						  urVo = new UserRoleVo();
						  urVo.setRoleId(rd.getRoleId());
						  urVo.setRoleName(rd.getRoleName());
						  urVo.setRemark(rd.getRemark());
						  urVo.setStatus(rd.getStatus());
						  urVo.setSelectStatus(false);
						  if(null != selectRoleList && selectRoleList.size()>0){
							  for(int j=0;j<selectRoleList.size();j++){
								  srd = selectRoleList.get(j);
								  if (rd.getRoleId() == srd.getRoleId()) {
									  urVo.setSelectStatus(true);//已经有权限
								  }
							  }
						  }
						  urList.add(urVo); 
					  }
					  return urList;
				  }
				  return null;
			  }
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}  
	}

	@Override
	public void saveUserAddRole(UserDto userDto,String roleIds) throws ServiceException {
		try {
			//删除当前用户绑定角色
			super.daoFacade.getUserDao().deleteUserRoleByUserId(userDto.getUserId()); 
			
			AdminRoleDto urDto = null;
			if (PublicUtil.isNotNullAndEmpty(roleIds)) {
				String [] ids = roleIds.split(",");
				for (int i = 0;i < ids.length; i++) {
					urDto = new AdminRoleDto();
					urDto.setUserId(userDto.getUserId());
					urDto.setRoleId(Integer.parseInt(ids[i])); 
					super.daoFacade.getUserDao().create(urDto);//插入角色权限
				}
			}
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public int loginUser(UserDto userDto) throws ServiceException {
		try {
			return super.daoFacade.getUserDao().loginUser(userDto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public boolean validateUserName(String userName) throws ServiceException {
		 
		try {
			return super.daoFacade.getUserDao().validateUserName(userName);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public void updateUserStatus(String userIds, Integer status) throws ServiceException {
		try {
			 UserDto ud = null;
			 if (!"".equals(userIds) && null != userIds) {
				  String userId [] = userIds.split(",");
				  if (null != userId) {
					  for (int i = 0;i < userId.length; i++) {
						  ud = (UserDto)super.daoFacade.getUserDao().get(UserDto.class, Integer.parseInt(userId[i]));
						  ud.setStatus(status);
						  super.daoFacade.getUserDao().update(ud);
					  }
				  }
			 } 
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public int saveUser(UserDto userDto) throws ServiceException {
		try {

			//userDto.setRestaurantId(rd.getId());//具体餐厅编号
			this.daoFacade.getUserDao().create(userDto);//保存餐厅管理员
			List<UserDto> userList = this.daoFacade.getUserDao().findBy(" from UserDto where 1=1 and userName = '"+userDto.getUserName()+"' ");
			
			if(null != userList){
				userDto = (UserDto)userList.get(0);
				RestaurantDto rd = new RestaurantDto();//餐厅实例
				rd.setName(userDto.getRealName());//用户名
				rd.setUserId(userDto.getUserId());//用户Id
				rd.setStatus(0);
				rd.setUserId(userDto.getUserId());//会员编号
				this.daoFacade.getRestaurantDao().create(rd);//保存餐厅实例
				List restList = this.daoFacade.getRestaurantDao().findBy(" from RestaurantDto where 1=1 and userId = " + userDto.getUserId());
				if(null != restList){
					rd = (RestaurantDto)restList.get(0);
					rd = (RestaurantDto)this.daoFacade.getRestaurantDao().get(RestaurantDto.class, rd.getId());
					userDto.setRestaurantId(rd.getId());
					this.daoFacade.getUserDao().saveOrUpdate(userDto);
				}
				
			}
			
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
		return 0;
	}
	
	/*
	 * 登陆角色查询方法
	 */
	public AdminRoleDto selectUserRole(Integer userId) throws ServiceException{
		try{
			return this.daoFacade.getUserDao().selectUserRole(userId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public UserDto selectUserOrAdmin(Integer userId) throws ServiceException{
		try{
			return this.daoFacade.getUserDao().selectUserOrAdmin(userId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
